/*----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the 
License at http://www.mozilla.org/MPL/
	
Software distributed under the License is distributed on 
an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, 
either express or implied. 
See the License for the specific language governing rights and 
limitations under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, 
software distributed under the License is distributed on 
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied.
See the License for the specific language governing permissions 
and limitations under the License.
----------------------------------------------------------------------------*/

package fever.data.validators 
{
	import com.bourre.collection.Queue;
	
	import fever.log.FeverDebug;	

	/**
	 * Performs a data validation process.
	 * 
	 * <p>Uses defined validation strategies to make correct validation 
	 * process.</p>
	 * 
	 * TODO Not complete
	 * 
	 * @see fever.data.validators.ValidatorStrategy
	 * @see fever.data.validators.strategy Fever Implemented strategies
	 * 
	 * @author Romain Ecarnot
	 */
	public class Validator
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		private var _strategy : ValidatorStrategy;
		private var _enabled : Boolean;
		private var _errorList : Queue;
		
		
		//--------------------------------------------------------------------
		// Public properties
		//--------------------------------------------------------------------
		
		/**
		 * Setting this value to <code>false</code> will stop the validator 
		 * from performing validation.
		 * 
		 * @default true
		 */
		public function get enabled() : Boolean { return _enabled; }
		/** @private */
		public function set enabled ( o : Boolean ) : void { _enabled = o; }
		
		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Constructor.
		 * 
		 * @param strategy Validation strategy to use. If not <code>null</code> 
		 * use the default <strong>ValidatorStrategy</strong> strategy.
		 * 
		 * @see fever.data.validators.ValidatorStrategy
		 * @see #setValidationStrategy()
		 */
		public function Validator( strategy : ValidatorStrategy = null )
		{
			_enabled = true;
			_errorList = new Queue( String );
			
			setValidationStrategy( strategy );
		}
		
		/**
		 * Sets validation process.
		 * 
		 * @param strategy Validation strategy to use. If not <code>null</code> 
		 * use the default <strong>ValidatorStrategy</strong> strategy.
		 * 
		 * @see fever.data.validators.ValidatorStrategy
		 */
		public function setValidationStrategy( strategy : ValidatorStrategy = null ) : void
		{
			if( strategy == null ) strategy = new ValidatorStrategy();
			
			_strategy = strategy;
			_strategy.setOwner( this );
		}
		
		/**
		 * Returns validation process.
		 */
		public function getValidationStrategy() : ValidatorStrategy
		{
			return _strategy;
		}	
		
		/**
		 * Validates passed-in argumets.
		 */
		public function validate( value : Object = null ) : void
		{
			_errorList.clear();
			
			//TODO Remove debug
			
			if( enabled )
			{
				FeverDebug.WARN( "Use stategy " + _strategy.toString() );
				
				_strategy.validate( value );
			}
			
			FeverDebug.WARN( "Validation [ " + value + " ] success " + _isValid() );
			
			if( !_isValid() )
				FeverDebug.WARN( "Errors " + _errorList.toArray() );
		}
		
		
		//--------------------------------------------------------------------
		// Private implementations
		//--------------------------------------------------------------------
		
		/**
		 * @private
		 * Returns <code>true</code> if errors list is empty.
		 */
		protected function _isValid( ) : Boolean
		{
			return _errorList.isEmpty();
		}
		
		/**
		 * @private
		 */
		protected function _pushError( errorString : String ) : void
		{
			_errorList.add( errorString  );	
		}
		
		/**
		 * @private
		 * Internal package communication
		 */
		internal function pushError( errorString : String ) : void
		{
			_pushError( errorString );
		}
	}
}
